//
//  EditCalViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/10/4.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "EditCalViewController.h"
#import "EditModel.h"

@interface EditCalViewController ()

@property (nonatomic, strong) UIButton *editBtn;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger btnTag;
@end

@implementation EditCalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviWith:@"更改店铺类别"];
    [self getData];
}

- (void)getData
{
    self.dataArray = [NSMutableArray array];
    [[HttpRequest currentHttpRequest]postPath:CategoryList_API params:nil resultBlock:^(id Data, MsgModel *msgModel) {
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:Data];
        for (NSInteger i = 0; i < array.count; i++) {
            NSDictionary *dic = array[i];
            EditModel *model = [EditModel new];
            model.categoryId = dic[@"id"];
            model.categoryName = dic[@"name"];
            [self.dataArray addObject:model];
        }
        EditModel *model = [EditModel new];
        model.categoryId = @"0";
        model.categoryName = @"海淘综合";
        [self.dataArray addObject:model];
        
         [self setMainView];
    }];
}

- (void)setMainView
{
    UIView *headView = [UIView new];
    if (@available(iOS 11.0, *)) {
        headView.frame = CGRectMake(0, TopBarHeight_xs, SCREENWIDTH, 44);
    } else {
        // Fallback on earlier versions
        headView.frame = CGRectMake(0, TopBarHeight, SCREENWIDTH, 44);
    }
    headView.backgroundColor = HexWithAlpha(0xF0F0F0, 0.53);
    [self.view addSubview:headView];
    
    UIImageView *leftIV = [UIImageView new];
    leftIV.frame = CGRectMake(15, 15, 14, 14);
    leftIV.image = [UIImage imageNamed:@"calSelect"];
    [headView addSubview:leftIV];
    
    UILabel *mobileLab = [UILabel new];
    mobileLab.frame = CGRectMake(leftIV.right+15, 11, 180, 21);
    mobileLab.textColor = UIColorFromHex(0x9E9E9E);
    mobileLab.font = FontSemiboldSize(14);
    mobileLab.text = [NSString stringWithFormat:@"当前店铺类别：%@",self.categoryName];
    [headView addSubview:mobileLab];
    
    UILabel *calLab = [UILabel new];
    calLab.frame = CGRectMake(15, headView.bottom+30, 26, 12);
    calLab.textColor = goldColor;
    calLab.font = FontSemiboldSize(12);
    calLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"类别"];
    [self.view addSubview:calLab];
    
    CGFloat padding = (SCREENWIDTH-3*82-50)/2;
    
    NSInteger quotient = self.dataArray.count/3;
    NSInteger remainder = self.dataArray.count%3;
    NSInteger index = remainder>0?quotient+1:quotient;
//    NSArray *array = @[@"亲子母婴",@"美妆护肤",@"轻奢",@"服饰",@"营养保健",@"数码家电"];
    CGFloat bottom = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < index; j++) {
            
            EditModel *model = self.dataArray[3*j+i];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(25+(82+padding)*i, calLab.bottom+19+42*j, 82, 22);
            btn.backgroundColor = UIColorFromHex(0xF7F7F7);
            btn.layer.cornerRadius = 10;
            btn.layer.masksToBounds = YES;
            btn.titleLabel.font = FontMediumWithSize(12);
            [btn setTitleColor:UIColorFromHex(0x9E9E9E) forState:UIControlStateNormal];
            [btn setTitle:model.categoryName forState:UIControlStateNormal];
            [btn setTitleColor:UIColorFromHex(0x444444) forState:UIControlStateSelected];
            btn.tag = 80+3*j+i;
            if (i == 0 && j == 0) {
                btn.selected = YES;
                btn.backgroundColor = goldColor;
            }
            if (j == index-1) {
                bottom = btn.top + btn.height;
            }
            [btn addTarget:self action:@selector(chooseBtn:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:btn];
        }
    }
    
    self.editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editBtn.frame = CGRectMake(2*leftPadding, bottom+70, SCREENWIDTH-80, 50);
    self.editBtn.layer.cornerRadius = 25;
    self.editBtn.layer.masksToBounds = YES;
    self.editBtn.backgroundColor = goldColor;
    self.editBtn.titleLabel.font = FontWithSize(18);
    [self.editBtn setTitleColor:UIColorFromHex(0x444444) forState:UIControlStateNormal];
    [self.editBtn setTitle:@"确认更改" forState:UIControlStateNormal];
    self.editBtn.userInteractionEnabled = YES;
    [self.editBtn addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.editBtn];
}

- (void)chooseBtn:(UIButton *)btn
{
    for (NSInteger i = 0; i < self.dataArray.count; i++) {
        if (btn.tag == 80+i) {
            btn.selected = YES;
            self.btnTag = btn.tag;
            btn.backgroundColor = goldColor;
//            self.editBtn.userInteractionEnabled = YES;
//            self.editBtn.backgroundColor = goldColor;
//            [self.editBtn setTitleColor:UIColorFromHex(0x444444) forState:UIControlStateNormal];
            continue;
        }
        UIButton *chooseBtn = (UIButton *)[self.view viewWithTag:i+80];
        chooseBtn.selected = NO;
        chooseBtn.backgroundColor = UIColorFromHex(0xF7F7F7);
    }
}

- (void)editClick:(UIButton *)btn
{
    NSLog(@"edit");
    NSInteger index = self.btnTag - 80;
    EditModel *model = self.dataArray[index];
    NSLog(@"tag=%@ \n name=%@",model.categoryId,model.categoryName);
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:model.categoryId forKey:@"mainCategoryId"];
    [dict setValue:model.categoryName forKey:@"mainCategoryName"];
    [HttpRequest formPostPath:EditMyInfo_API params:dict resultBlock:^(id Data, MsgModel *msgModel) {
       
        [self ShowHUDWithMessage:@"修改类别成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
}

@end
